<script setup>
import { useProductsStore } from '@/stores/products';
import { storeToRefs } from 'pinia';
import { getproductsList } from '@/apis/product';

const productsStore = useProductsStore();
const { products } = storeToRefs(productsStore);

// 处理页码改变的方法
const handlePageChange = async (newPage) => {
  console.log('New page:', newPage);
  try {
    // 调用 API 获取新一页的商品列表
    const res = await getproductsList(newPage, 8);
    console.log('Products list:', res);
    // 更新 Pinia store 中的商品列表和分页信息
    productsStore.setProducts(res.records);
    productsStore.setPage(res.total, res.size, newPage);
  } catch (error) {
    console.error('Error fetching products list:', error);
  }
};

</script>

<template>
  <div class="products-new">
    <h3 class="products-new-title">商品列表</h3>
    <div class="products-container">
      <div class="product-empty" v-if="products.list.length === 0">
        <el-empty description="没有找到相关宝贝"></el-empty>
      </div>
      <div class="product" v-else v-for='product in products.list' :key="product.id">
        <router-link :to="'/product/' + product.id" class="product-link">
          <figure class="product-figure">
            <img :src="'/api/images/' + product.mainImage" alt="图片" />
            <figcaption class="product-title">
              {{ product.name }}
            </figcaption>
            <p class="product-price">
              {{ product.price }}元
            </p>
            <p class="product-subtitle">{{ product.subTitle }}</p>
          </figure>
        </router-link>
      </div>
    </div>
  </div>
  <div class="product-list-pagination">
    <el-pagination v-model:currentPage="products.current" :page-size="products.pageSize" :total="products.total"
      layout="total, prev, pager, next" @current-change="handlePageChange"></el-pagination>
  </div>
</template>


<style scoped lang="scss">
.products-new {
  width: 90%;
  margin: 0 auto;
  padding: 2rem 0;
  border-radius: 2rem;
  background-color: #fff;

  .products-new-title {
    font-size: 2rem;
    font-weight: bold;
    margin-bottom: 2rem;
    margin-left: 2rem;
    font-family: PingFang SC, Microsoft YaHei, sans-serif;
  }

  .products-container {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    grid-column-gap: 1rem; // 设置每列之间的间隔
    width: 100%;
    .product-empty{
      width: 100%;
      height: 100%;
      display: flex;
      justify-content: center;
      align-items: center;
      grid-column: 1 / -1;
    }
    .product {
      flex: 1;
      display: inline-block;
      width: 19rem;
      margin-left: 1rem;
      text-align: center;

      .product-figure {
        height: 20rem;

        .product-title {
          //设置只显示一行
          overflow: hidden;
          white-space: nowrap;
          text-overflow: ellipsis;
          font-size: 1.2rem;
          margin-top: 1rem;
        }

        .product-price {
          color: #ff6700;
          font-size: 1.4rem;
          margin-top: 1rem;
        }
      }

      img {
        width: 10rem;
        height: 12rem;
      }

      span {
        color: #cdcdcd;
        text-decoration: line-through;
      }

      a {
        display: inline-block;
        width: 100%;
        height: 100%;
        border: 1px solid rgba(255, 255, 255, 0.5);
        border-radius: 1rem;
        padding: 1rem;
        box-sizing: border-box;
        &:hover {
          cursor: pointer;
          /**设置边框 */
          border: 1px solid #ff6700;
          border-radius: 1rem;
        }

      }
    }
  }
}

.product-list-pagination {
  margin-top: 20px;
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
}
</style>
